package com.yangxinxin.lesson.service.implement;

import com.yangxinxin.lesson.constant.Constant;
import com.yangxinxin.lesson.service.CodeService;
import com.yangxinxin.lesson.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
@Service
public class CodeServiceImpl implements CodeService {
    /**
     *
     */
    @Autowired
    private RedisService redisService;
    @Override
    public String getOrderCode(String type) {
        SimpleDateFormat simpleDateFormat  =  new SimpleDateFormat("yyyyMMdd");
        String data = simpleDateFormat.format(System.currentTimeMillis());
        Long number = redisService.incrby(Constant.ORDER_CODE_KEY+data,1);
        String pad = padRight(number.toString(),7,"0");
        return data+type+pad;
    }
    //因为要补齐
    private  String padRight(String  oldStr,int len,String alexin){
        String str="";
        int strlen = oldStr.length();
        for (int i=0;i<len -  strlen;++i){
            str= str + alexin;
        }
        str = str + oldStr;
        return  str;
    }
}
